﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;

namespace DAL
{
    public class adminService
    {
        public static admin SelectByNameAndPwd(string adminName, string pwd)
        {
            using (EasyDBEntities db = new EasyDBEntities())
            {
                return db.admin.SingleOrDefault(a=a>.adminname== adminName && a.pwd == pwd);
            }
        }
        //用户登录
        public static User SelectuserByNameAndPwd(string userName, string pwd)
        {
            using (EasyDBEntities db = new EasyDBEntities())
            {
                return db.admin.SingleOrDefault(a = a >.adminname == name && a.pwd == pwd);
            }
        }
        //查询所有用户的方法
        public static List<User> SelelctAll()
        {
            using (EasyDBEntities db = new EasyDBEntities())
            {
                return db.User.ToList();
            }
        }
        //精确查找
        public static User SelectById(int id)
        {
            using (EasyDBEntities db = new EasyDBEntities())
            {
                return db.User.SingleOrDefault(a = a >.userid==id);
            }
        }
        //增加
        public static bool Insert(User user)
        {
            using (EasyDBEntities db = new EasyDBEntities())
            {
                db.User.Add(user);
                return db.SaveChanges() > 0;
            }
        }
        //更改
        public static bool update(users user)
        {
            using (EasyDBEntities db = new EasyDBEntities())
            {
                
                db.users.Remove(user);
                return db.SaveChanges() > 0;
            }
        }
        //删除
        public static bool Delete(int id)
        {
            using (EasyDBEntities db = new EasyDBEntities())
            {
                var user = db.users.SingleOrDefault(u => u.userid = id);
                if (user==null)
                {
                    return false;
                }
                db.users.Remove(user);
                return db.SaveChanges() > 0;
            }  
        }
    }
}
